﻿using System.Collections.Generic;
using System.Linq;
using IGM.DataProvider;
using IGM.Framework.Constants;

namespace IGM.Business
{
    public partial class Business
    {
        #region -- Get Subcommittee Group --

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<SubcommitteeGroup> GetSubcommitteeByGroupId(int groupId)
        {
            return DataContex != null && groupId > 0
                        ? DataContex.SubcommitteeGroups.Where(m => m.IdGroup == groupId).ToList() : null;
        }

        public List<SubcommitteeGroup> GetGroupBySubcommitteeId(int subcommitteeId)
        {
            return DataContex != null && subcommitteeId > 0
                        ? DataContex.SubcommitteeGroups.Where(m => m.IdSubcommittee == subcommitteeId).ToList() : null;
        }

        public List<SubcommitteeGroup> GetSubcommitteeGroupByMajorId(int majorId)
        {
            return DataContex != null && majorId > 0
                        ? DataContex.SubcommitteeGroups.Where(m => m.Group.IdMajor == majorId).ToList()
                        : null;
        }

        #endregion

        #region -- Add Subcommittee Group--

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subcommitteeGroup"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddSubcommitteeGroupValidator(SubcommitteeGroup subcommitteeGroup, out string message)
        {
            if (DataContex.SubcommitteeGroups.Any(f => f.IdSubcommittee == subcommitteeGroup.IdSubcommittee && f.IdGroup == subcommitteeGroup.IdGroup))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "Nhóm thuộc tiểu ban");
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        public void AddSubcommitteeGroup(SubcommitteeGroup subcommitteeGroup)
        {
            DataContex.SubcommitteeGroups.AddObject(subcommitteeGroup);
            DataContex.SaveChanges();
        }

        #endregion

        #region -- Edit Subcommittee Group --


        public bool EditSubcommitteeGroupValidator(SubcommitteeGroup subcommitteeGroup, out string message)
        {
            var id = subcommitteeGroup.Id;

            if (DataContex.SubcommitteeGroups.Any(f => f.IdSubcommittee == subcommitteeGroup.IdSubcommittee && f.IdGroup == subcommitteeGroup.IdGroup && f.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "Nhóm thuộc tiểu ban");
                return false;
            }
            message = string.Empty;
            return true;
        }

        public bool EditSubcommitteeGroup(SubcommitteeGroup subcommitteeGroup)
        {
            var id = subcommitteeGroup.Id;
            var updateSubcommitteeGroupr = DataContex.SubcommitteeGroups.SingleOrDefault(f => f.Id == id);

            if (updateSubcommitteeGroupr == null)
                return false;

            updateSubcommitteeGroupr.IdGroup = updateSubcommitteeGroupr.IdGroup;

            DataContex.SaveChanges();
            return true;
        }

        #endregion

        #region -- Delete Subcommittee Group --

        public bool DeleteSubcommitteeGroupValidator(IEnumerable<SubcommitteeGroup> subcommitteeGroups, out string message)
        {
            const string intialize = "Nhóm thuộc tiểu ban:";
            var itemsCannotIdentify = (from subcommittee1 in subcommitteeGroups
                                       let id = subcommittee1.Id
                                       where !DataContex.SubcommitteeGroups.Any(m => m.Id == id)
                                       select subcommittee1).Aggregate(intialize, (current, subcommittee1) => current + (subcommittee1.IdSubcommittee + ", "));

            if (itemsCannotIdentify != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsCanNotIdentify, itemsCannotIdentify.TrimEnd(','));
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// Delete object
        /// </summary>
        public void DeleteSubcommitteeGroup(List<SubcommitteeGroup> subcommitteeGroups)
        {
            for (var i = subcommitteeGroups.Count - 1; i >= 0; i--)
            {
                var id = subcommitteeGroups[i].Id;
                var deletesubcommittee = DataContex.SubcommitteeGroups.Single(m => m.Id == id);
                DataContex.SubcommitteeGroups.DeleteObject(deletesubcommittee);
            }
            DataContex.SaveChanges();
        }

        #endregion

    }
}